package com.goods.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.goods.dao.WuzichukuDao;
import com.goods.entity.WuzichukuEntity;
import com.goods.model.WuzichukuVO;
import com.goods.model.WuzichukuView;
import com.goods.service.WuzichukuService;
import com.goods.utils.PageUtils;
import com.goods.utils.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.List;

@Service("wuzichukuService")
public class WuzichukuServiceImpl extends ServiceImpl<WuzichukuDao, WuzichukuEntity> implements WuzichukuService {
	
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<WuzichukuEntity> page = this.selectPage(
                new Query<WuzichukuEntity>(params).getPage(),
                new EntityWrapper<WuzichukuEntity>()
        );
        return new PageUtils(page);
    }
    
    @Override
	public PageUtils queryPage(Map<String, Object> params, Wrapper<WuzichukuEntity> wrapper) {
		  Page<WuzichukuView> page =new Query<WuzichukuView>(params).getPage();
	        page.setRecords(baseMapper.selectListView(page,wrapper));
	    	PageUtils pageUtil = new PageUtils(page);
	    	return pageUtil;
 	}
    
    @Override
	public List<WuzichukuVO> selectListVO(Wrapper<WuzichukuEntity> wrapper) {
 		return baseMapper.selectListVO(wrapper);
	}
	
	@Override
	public WuzichukuVO selectVO(Wrapper<WuzichukuEntity> wrapper) {
 		return baseMapper.selectVO(wrapper);
	}
	
	@Override
	public List<WuzichukuView> selectListView(Wrapper<WuzichukuEntity> wrapper) {
		List<WuzichukuView> wuzichukuViews = baseMapper.selectListView(wrapper);
		sortList(wuzichukuViews,0,wuzichukuViews.size()-1);
		return wuzichukuViews;
	}

	/**
	 * 排序
	 * @param wuzichukuViews
	 */
	private void sortList(List<WuzichukuView> wuzichukuViews, int begin, int end) {
		int middle,i,j;
		i = begin;
		j = end;
		if(begin > end){
			return;
		}
		middle = wuzichukuViews.get(begin).getChukushuliang();
		while (i < j){
			if(i < j && wuzichukuViews.get(j).getChukushuliang() >= middle){
				j--;
			}
			if(i < j && wuzichukuViews.get(i).getChukushuliang() <= middle){
				i++;
			}
			if(i < j){
				swap(wuzichukuViews,i,j);
			}
		}
		swap(wuzichukuViews,begin,i);
		sortList(wuzichukuViews,begin,i-1);
		sortList(wuzichukuViews,i+1,end);
	}
	private static void swap(List<WuzichukuView> data,int from,int to){
		WuzichukuView temp = data.get(from);
		data.set(from,data.get(to));
		data.set(to,temp);
	}

	@Override
	public WuzichukuView selectView(Wrapper<WuzichukuEntity> wrapper) {
		return baseMapper.selectView(wrapper);
	}

    @Override
    public List<Map<String, Object>> selectValue(Map<String, Object> params, Wrapper<WuzichukuEntity> wrapper) {
        return baseMapper.selectValue(params, wrapper);
    }

    @Override
    public List<Map<String, Object>> selectTimeStatValue(Map<String, Object> params, Wrapper<WuzichukuEntity> wrapper) {
        return baseMapper.selectTimeStatValue(params, wrapper);
    }

    @Override
    public List<Map<String, Object>> selectGroup(Map<String, Object> params, Wrapper<WuzichukuEntity> wrapper) {
        return baseMapper.selectGroup(params, wrapper);
    }




}
